*===============================================================================
*Firm and Country Embedded Productivity (Alviarez, Cravino and Ramondo) 
*Figure 2 (DA/DP relative to labor productivity)
*Last Modified by Vanessa Alviarez 0/2022
*===============================================================================

/*
*--------------------------------------------------------------
global typeden=1
include "TFP_firm_directory_historical.do"
	
local typeused "agg"	
local title "naics_sales_s4_base_woparent"
use "${output}/estimates_`typeused'_`title'.dta", clear	
	
local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rho=8.5
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A D_P {
gen `var'_phi=(-1)*`var'/(`rho' -1)
*Computing (b-b') using TFP
*Fixing the labor share in 2/3
*---------------------------------------
replace inv_labsh`rr'=1.5
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - inv_labsh`rr'*`var'_phi
}
replace D_A_phi=D_A_phi*inv_labsh`rr'
replace D_P_phi=D_P_phi*inv_labsh`rr'

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"

*gen ind=0
*foreach xx in GR PL MX ES PT IT FR GB JP DE SE {
*replace ind=1 if isocode=="`xx'"
*}
*keep if ind==1

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	display "`fig2_lp_`title'`var2'_var`grvar0'`yy'"	
	
*/

	
	
*---------------------------------------------
*Aggregated*
*---------------------------------------------
local grnum=9 /*3 5 6 7 8 9 10*/
local growth_graph=0
local grvar0 "gdp_pw_ppp" /*gdp_pw_ppp "lp" "LP_Q_ppp_emp_pwt", "LP_Q_usd_emp"; "rtfpna"; ..."LP1TFP_I_klems" ctfp gdp_pw_ppp gdp_pc_ppp gdp_pc_curr LP_Q_ppp LP_Q_ppp_emp  gdp_pw_real gdp_pc_real*/
local grvar1 "" /* "gr`grnum'y_ln_`grvar0'" */
display "`grvar1'"

*This is in headings (called in the master file) Erase it after it has its name in headings do file
local ending "" /*"_10plus", "_withLF" ,"_gap" and ""*/
local zz naics  /*naics, naics3, naics4*/
local var0 "_sales" 
local name "_base" /*"_base", "_exclnomark", "_growth`grnum'years" */
local name1 "_woparent" /* "_woparent_BTT" "_woparent_tax_ratio"  "_woparent"  "_woparent_gdpdiff" "_abovep50", "_decile5060" "_growth`grnum'years", "_fullgrav", "_nograv","_dist2", "_gdpgap" "_pctile`x'", "_woGUOFE", "_woisoFE", "_min`xx'years", "_LHSlevels", "_growth`xx'years", ALL OF WHICH ARE INDIVIDUAL DEPARTURES FROM THE BASELINE*/
local samp=4  /*samp2 includes AU, DK and NO*/
local typeused "agg" /*"agg"*/
local var2 "" /* _DP*/
local var3 "A"
local title "`zz'`var0'_s`samp'`name'`name1'`ending'"
display "`title'"
global ctryrel FR

local rho=8.5
local rr "" /*"_fr_pwt", "_ctryn", "_fr", "" */
display "inv_labsh`rr'"
local extra "" /* labsh`rr' */
*---------------------------------------

local yy "2016"
local yy_ref=`yy'
local nn=10
local sample `" isocode=="IN"  | isocode=="TR" "'
*local sample `" isocode=="TR" | isocode=="IN" | isocode=="BR" | isocode=="US" | isocode=="DK" "'

clear all
use "${output}/estimates_sec_`title'.dta", clear
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 

display "estimates_`typeused'_`title'"
use "${output}/estimates_`typeused'_`title'.dta", clear
keep if year==`yy'
$condtype
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
drop if D_A==. | (D_A==0 & isocode!="FR")
drop if `sample'  
tab type


/*
keep year isocode type D_A D_A_se D_P D_P_se
foreach vv in D_A D_A_se D_P D_P_se {
rename `vv' `vv'_org
*rename `vv' `vv'_BTT
}
save "${output}/temp0.dta", replace
*-------------------------------------------------------------
use "${output}/temp0.dta", clear
merge 1:1 year isocode type using "${output}/temp1.dta"
drop _merge
erase  "${output}/temp0.dta"
erase  "${output}/temp1.dta"
*-------------------------------------------------------------
*/



if `growth_graph'==0 {
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A D_P {
gen `var'_phi=(-1)*`var'/(`rho' -1)
*Computing (b-b') using TFP
*Fixing the labor share in 2/3
*---------------------------------------
replace inv_labsh`rr'=1.5
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - inv_labsh`rr'*`var'_phi
}
replace D_A_phi=D_A_phi*inv_labsh`rr'
replace D_P_phi=D_P_phi*inv_labsh`rr'

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
}
*


if `growth_graph'==1 {

drop if isocode=="RO"
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg_growth0.dta"
drop if _merge==2
drop _merge
keep year isocode type sector1 D_A* D_P* inv_labsh* *`grvar0'*

foreach var in D_A D_P {
gen `var'_phi=(-1)*`var'/(`rho' -1)
*Computing (b-b') using TFP
gen b_`var'_phi_`grvar0'=`grvar1' - inv_labsh`rr'*`var'_phi
}
replace D_A_phi=D_A_phi*inv_labsh`rr'
replace D_P_phi=D_P_phi*inv_labsh`rr'

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS `grvar1'
}
*

sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"

*gen ind=0
*foreach xx in GR PL MX ES PT IT FR GB JP DE SE {
*replace ind=1 if isocode=="`xx'"
*}
*keep if ind==1

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	display "`fig2_lp_`title'`var2'_var`grvar0'`yy'"	

	*br isocode `LHS1' `RHS'
	*graph export "${results}/figures/fig2_lp_`title'`var2'_var`grvar0'`yy'`extra'_select.pdf", replace
	graph export "${results}/figures/fig2_lp_`title'`var2'_var`grvar0'`yy'`extra'.pdf", replace

	
	

/*
local ylab ""
local xlab ""
foreach n in -1.00 -0.50 0.00 0.50 {
	local ylab `ylab' `n' "`=string(exp(`n'),"%9.1f")'"
	display `"`ylab'"'
}
*forvalues n = -1(0.2)0.2 {
foreach n in -1.098622289 -0.693147289 -0.287672289 0.117802711 {
	local xlab `xlab' `n' "`=string(exp(`n'),"%9.2f")'"
}

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(`ylab', format(%5.1f) labsize(medlarge)) xlabel(`xlab', format(%9.2f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	display "`fig2_lp_`title'`var2'_var`grvar0'`yy'"	
	graph export "${results}/figures/fig2_lp_`title'`var2'_var`grvar0'`yy'`extra'.pdf", replace
	*graph export "${results}/figures/fig2_lp_`title'`var2'_var`grvar0'`yy'`extra'_fixlabsh.pdf", replace
*/

/*
global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(medium) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(medium) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(, format(%5.1f) labsize(medlarge)) xlabel(, format(%5.1f) labsize(medlarge)) ///
	title("Fig2 (lp): `title' (var: `grvar0', `yy')", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	display "`fig2_lp_`title'"
	graph export "${results}/figures/fig2_lp_var`grvar0'`yy'_`title'.pdf", replace
*/



*---------------------------------------------
*Aggregated (market share (exp DA))*
*---------------------------------------------
local grnum=9 /*3 5 6 7 8 9 10*/
local growth_graph=0
local grvar0 "gdp_pw_ppp" /*gdp_pw_ppp "lp" "LP_Q_ppp_emp_pwt", "LP_Q_usd_emp"; "rtfpna"; ..."LP1TFP_I_klems" ctfp gdp_pw_ppp gdp_pc_ppp gdp_pc_curr LP_Q_ppp LP_Q_ppp_emp  gdp_pw_real gdp_pc_real*/
local grvar1 "" /* "gr`grnum'y_ln_`grvar0'" */
display "`grvar1'"

*This is in headings (called in the master file) Erase it after it has its name in headings do file
local ending "_TURN" /*"_10plus", "_withLF" ,"_gap" and ""*/
local zz naics  /*naics, naics3, naics4*/
local var0 "_sales" 
local name "_base" /*"_base", "_exclnomark", "_growth`grnum'years" */
local name1 "_woparent" /* "_woparent_hqsecFE" "_abovep50", "_decile5060" "_growth`grnum'years", "_fullgrav", "_nograv","_dist2", "_gdpgap" "_pctile`x'", "_woGUOFE", "_woisoFE", "_min`xx'years", "_LHSlevels", "_growth`xx'years", ALL OF WHICH ARE INDIVIDUAL DEPARTURES FROM THE BASELINE*/
local samp=4  /*samp2 includes AU, DK and NO*/
local typeused "agg" /*"agg"*/
local var2 "" /* _DP*/
local var3 "A"
local title "`zz'`var0'_s`samp'`name'`name1'`ending'"
display "`title'"
global ctryrel FR

local rho=8.5
local rr "" /*"_fr_pwt", "_ctryn", "_fr", "" */
display "inv_labsh`rr'"
local extra "mktshare_uncond" /* labsh`rr' */
*---------------------------------------

local yy "2016"
local yy_ref=`yy'
local nn=10
local sample `" isocode=="IN"  | isocode=="TR" "'
*local sample `" isocode=="TR" | isocode=="IN" | isocode=="BR" | isocode=="US" | isocode=="DK" "'

clear all
use "${output}/estimates_sec_`title'.dta", clear

by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 

use "${output}/estimates_`typeused'_`title'.dta", clear
keep if year==`yy'
$condtype
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
drop if D_A==. | (D_A==0 & isocode!="FR")
drop if `sample'  
tab type


if `growth_graph'==0 {
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A D_P {
gen `var'_phi=(-1)*`var'/(`rho' -1)
*Computing (b-b') using TFP
gen b_`var'_phi_`grvar0'=ln_`grvar0' - inv_labsh`rr'*`var'_phi
}
replace D_A_phi=D_A_phi*inv_labsh`rr'
replace D_P_phi=D_P_phi*inv_labsh`rr'
gen DA_exp=D_A

local LHS0 DA_exp
local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
}
*


sum `LHS1' `RHS'  `LHS2'
local ytitle ""
local xtitle "Relative output per worker"
local firm_emb "" 
local ctry_emb ""


reg `LHS1' `RHS' 
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2
	
local ylab ""
local xlab ""
*forvalues n= 0(2)12 {
*foreach n in -0.693147181 0 0.693147181 1.386294361 2.079441542 2.772588722 3.465735903  {
foreach n in -0.693147181 0 0.693147181 1.386294361 2.079441542 2.772588722   {
	local ylab `ylab' `n' "`=string(exp(`n'),"%9.0f")'"
	display `"`ylab'"'
}
*forvalues n = 0.4(0.2)1 {
foreach n in -1.098622289 -0.693147289 -0.287672289 0.117802711 {
*foreach n in -0.916290732 -0.510825624 -0.223143551  0 {
	local xlab `xlab' `n' "`=string(exp(`n'),"%9.2f")'"
}
*local ylab ""

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS0' `RHS', sort $opt2  mcolor(blue)) (line `RHS' `RHS', lcolor(none)), ylabel(`ylab', format(%5.1f) labsize(medium)) xlabel(`xlab', format(%5.1f) labsize(medium)) ///
	title("", size(small)) ytitle("`ytitle'", size(medium)) xtitle("`xtitle'", size(medium)) graphregion(fcolor(white) lcolor(white) ) ///
	legend(off) 

	display "`fig2_lp_`title'`var2'_var`grvar0'`yy'"	
	

	graph export "${results}/figures/fig2_lp_`title'`var2'_var`grvar0'`yy'`extra'.pdf", replace

br isocode `LHS0' `RHS'
	
	
	
	
	
*---------------------------------------------
*Agg Type Output*
*---------------------------------------------
set more off
local grnum=5 
local growth_graph=0
local grvar0 "LP_ppp_emp_pwt" 
local grvar1 "gr`grnum'y_ln_`grvar0'"

*This is in headings (called in the master file) Erase it after it has its name in headings do file
local zz naics  
local var0 "_sales" 
local name "_base" 
local name1 "_woparent" 
local samp=4
local ending "" 
local typeused "aggtype" 
local var2 "" 
local var3 "A"
global condtype "" 

local title "`zz'`var0'_s`samp'`name'`name1'`ending'"
global ctryrel FR
local rr "" 
display "inv_labsh`rr'"
local rho=8.5
*---------------------------------------

local yy "2016"
local yy_ref=`yy'
local nn=10
local sample `" isocode=="IN" | isocode=="TR" "'
*local sample `" isocode=="IN" | isocode=="TR" | isocode=="US" | isocode=="DK" | isocode=="BR" "'


foreach ss in manuf  serv {
display "`ss'"

clear all
use "${output}/estimates_sec_`title'.dta", clear
keep if type=="`ss'" & num_aff>=3 
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)

if "`ss'"=="manuf" {
keep if num>=4 
}
if "`ss'"=="serv" {
keep if num>=4 
}
keep year isocode
duplicates drop 
tempfile iso_`ss'
save `iso_`ss'', replace 
}
*
use "${output}/estimates_`typeused'_`title'.dta", clear
levelsof type, local(typelist)

foreach selecsec in manuf  {
display "`selecsec'"
use "${output}/estimates_`typeused'_`title'.dta", clear
keep if year==`yy' & type=="`selecsec'"
$condtype
merge m:1 year isocode using `iso_`selecsec''
keep if _merge==3
drop _merge
capture rename DA D_A
capture rename DP D_P
drop if D_A==. | (D_A==0 & isocode!="FR")
drop if `sample'  

if `growth_graph'==0 {
display "LEVELS"

replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A D_P {
gen `var'_phi=(-1)*`var'/(`rho' -1)
*Computing (b-b') using TFP
*Fixing the labor share in 2/3
*---------------------------------------
replace inv_labsh`rr'=1.5
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - inv_labsh`rr'*`var'_phi
}
replace D_A_phi=D_A_phi*inv_labsh`rr'
replace D_P_phi=D_P_phi*inv_labsh`rr'

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
}
*


if `growth_graph'==1 {

drop if isocode=="RO"
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg_growth0.dta"
drop if _merge==2
drop _merge

keep year isocode type sector1 D_A* D_P* inv_labsh* *`grvar0'*
foreach var in D_A D_P {
gen `var'_phi=(-1)*`var'/(`rho' -1)
*Computing (b-b') using TFP


gen b_`var'_phi_`grvar0'=`grvar1' - inv_labsh`rr'*`var'_phi
}
replace D_A_phi=D_A_phi*inv_labsh`rr'
replace D_P_phi=D_P_phi*inv_labsh`rr'

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS `grvar1'
}
*

drop if isocode=="IN"
sum `LHS1' `RHS' `LHS2'

local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"


reg `LHS1' `RHS' 
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2

/*

local ylab ""
local xlab ""
foreach n in -1.00 -0.50 0.00 0.50 {
	local ylab `ylab' `n' "`=string(exp(`n'),"%9.1f")'"
	display `"`ylab'"'
}
*forvalues n = -1(0.2)0.2 {
*foreach n in  -1.098622289 -0.693147289 -0.287672289 0.117802711  {
foreach n in -0.693147289 -0.287672289 0.117802711 0.523277711 {
	local xlab `xlab' `n' "`=string(exp(`n'),"%9.2f")'"
}

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(`ylab', format(%5.1f) labsize(medlarge)) xlabel(`xlab', format(%9.2f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	display "`fig2_lp_`title'`var2'_var`grvar0'`yy'"
	graph export "${results}/figures/fig2_lp_`title'`var2'_var`grvar0'`yy'_`selecsec'.pdf", replace
*/

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `LHS1' `LHS1', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `LHS1' `LHS1', lcolor(none)),   xscale(titlegap(*1))  ylabel(-1(0.5)0.5, format(%5.1f) labsize(medlarge)) xlabel(-1.2(0.4)0.4, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) /// yscale(range(-1 0.5)) ylabel(-1(0.5)0.5,)
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 
	display "fig2_lp_`title'_var`grvar0'`yy'_`selecsec'"
		
	display "`fig2_lp_`title'`var2'_var`grvar0'`yy'"
	graph export "${results}/figures/fig2_lp_`title'`var2'_var`grvar0'`yy'_`selecsec'.pdf", replace
}
*

br isocode `LHS1' `RHS'
sort isocode
	
	
*---------------------------------------------
*Sectoral Output*
*---------------------------------------------
/*
                   sector |      Freq.     Percent        Cum.
---------------------------+-----------------------------------
   Accomod_Recreat (I-R-S) |         26        5.59        5.59
           AgrMining (A-B) |         26        5.59       11.18
      Basic Metals (24-25) |         26        5.59       16.77
         ChePetPla (19-23) |         26        5.59       22.37
          Construction (F) |         26        5.59       27.96
             Education (P) |         19        4.09       32.04
          ElecMach (26-28) |         26        5.59       37.63
         Electricity (D-E) |         26        5.59       43.23
   Financial_Insurance (K) |         26        5.59       48.82
              Food (10-12) |         26        5.59       54.41
                Health (Q) |         24        5.16       59.57
           Information (J) |         26        5.59       65.16
 Public_Administration (O) |          7        1.51       66.67
           Real_Estate (L) |         25        5.38       72.04
    Support_Services (M-N) |         26        5.59       77.63
           TexWood (13-18) |         26        5.59       83.23
  TranspOtherManuf (29-33) |         26        5.59       88.82
Transportation_Storage (H) |         26        5.59       94.41
      Wholesale_Retail (G) |         26        5.59      100.00
---------------------------+-----------------------------------
                     Total |        465      100.00
*/
			
local grnum=5 /*3 5 6 7*/
local growth_graph=0
local grvar0 "LP_ppp_emp_pwt" /*"rtfpna"; ..."LP1TFP_I_klems" ctfp gdp_pw_ppp gdp_pc_ppp gdp_pc_curr LP_Q_ppp LP_Q_ppp_emp  gdp_pw_real gdp_pc_real*/
local grvar1 "gr`grnum'y_ln_`grvar0'"

*This is in headings (called in the master file) Erase it after it has its name in headings do file
local zz naics  /*naics, naics3, naics4*/
local var0 "_sales" 
local name "_base" /*"_base", "_exclnomark", "_growth`grnum'years" */
local name1 "_woparent" /* "_fullgrav", "_nograv","_dist2", "_gdpgap" "_pctile`x'", "_woGUOFE", "_woisoFE", "_min`xx'years", "_LHSlevels", "_growth`xx'years", ALL OF WHICH ARE INDIVIDUAL DEPARTURES FROM THE BASELINE*/
local samp=4  /*samp2 includes AU, DK and NO*/
local ending "_TURN" /*"_gap" and ""*/
local typeused "sec" /*"agg"*/
global condtype "" /*`" keep if type=="`typeused2'" "'*/

local title "`zz'`var0'_s`samp'`name'`name1'`ending'"
global ctryrel FR
local rho=8.5
local rr ""
*---------------------------------------

local yy "2016"
local yy_ref=`yy'
local nn=10
local sample `" isocode=="" "'


clear all
use "${output}/estimates_sec_`title'.dta", clear
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 

use "${output}/estimates_`typeused'_`title'.dta", clear
drop if sector1==sector
keep if type=="manuf" | type=="serv"
*keep if type=="serv"
replace DA=. if num_aff<3
replace DP=. if num_aff<3
levelsof sector, local(seclist)



foreach selecsec of local seclist {
display "`selecsec'"
preserve
keep if year==`yy' & sector=="`selecsec'"
$condtype
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
rename DA D_A
rename DP D_P
drop if D_A==. | (D_A==0 & isocode!="FR")
drop if `sample'  

if `growth_graph'==0 {
replace year=`yy_ref'
merge m:1 year isocode sector using "${output}/aggregates_tfp_lp_klems.dta" 
drop if _merge==2
drop _merge


foreach var in D_A D_P {
gen `var'_phi=(-1)*`var'/(`rho' -1)
*Computing (b-b') using TFP
*Fixing the labor share in 2/3
*---------------------------------------
replace inv_labsh`rr'=1.5
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - inv_labsh`rr'*`var'_phi
}
replace D_A_phi=D_A_phi*inv_labsh`rr'
local LHS1 D_A_phi
local LHS2 b_D_A_phi_`grvar0'
local RHS ln_`grvar0'
}
*


if `growth_graph'==1 {

drop if isocode=="RO"
replace year=`yy_ref'
merge m:1 year isocode sector using "${output}/aggregates_tfp_lp_klems_growth0.dta" /*Not done year*/
drop if _merge==2
drop _merge

keep year isocode type sector1 D_A* D_P* inv_labsh* *`grvar0'*
foreach var in D_A D_P {
gen `var'_phi=(-1)*`var'/(`rho' -1)
*Computing (b-b') using TFP
*Fixing the labor share in 2/3
*---------------------------------------
replace inv_labsh`rr'=1.5
*---------------------------------------
gen b_`var'_phi_`grvar0'=`grvar1' - inv_labsh`rr'*`var'_phi
}
replace D_A_phi=D_A_phi*inv_labsh`rr'
local LHS1 D_A_phi
local LHS2 b_D_A_phi_`grvar0'
local RHS `grvar1'
}
*
sum `LHS1' `RHS' 
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"

reg `LHS1' `RHS' 
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2

/*	
local ylab ""
local xlab ""
foreach n in -1.5 -1.00 -0.50 0.00 0.50 1 {
	local ylab `ylab' `n' "`=string(exp(`n'),"%9.1f")'"
	display `"`ylab'"'
}
*forvalues n = -1(0.2)0.2 {
*foreach n in -1.504097289  -1.098622289 -0.693147289 -0.287672289 0.117802711 {
foreach n in   -1.098622289 -0.693147289 -0.287672289 0.117802711 0.523277711  {
	local xlab `xlab' `n' "`=string(exp(`n'),"%9.2f")'"
}

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(`ylab', format(%5.1f) labsize(medlarge)) xlabel(`xlab', format(%9.2f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	display "`fig2_lp_`title'`var2'_var`grvar0'`yy'"
	
	graph export "${results}/figures/fig2_lp_`title'_var`grvar0'`yy'_`selecsec'.pdf", replace
restore
*/

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(medium) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(medium) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  xscale(titlegap(*1)) ylabel(-1.5(0.5)1, format(%5.1f) labsize(medlarge)) xlabel(, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	display "fig2_lp_`title'_var`grvar0'`yy'_`selecsec'"	
	graph export "${results}/figures/fig2_lp_`title'_var`grvar0'`yy'_`selecsec'.pdf", replace
restore

/*	
	global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(medium) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(medium) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  xscale(titlegap(*1)) ylabel(, format(%5.1f) labsize(medlarge)) xlabel(, format(%5.1f) labsize(medlarge)) ///
	title("Fig2 (lp): `title' [`selecsec'] (var: `grvar0', `yy')", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	display "`fig2_lp_`title'"
	
	graph export "${results}/figures/fig2_lp_var`grvar0'`yy'_`title'_`selecsec'.pdf", replace

restore
*/
}
*

